<template>
	<view>
		<uni-nav-bar style="width: 100%;" fixed="true" left-icon="back"
		 @clickLeft="toBack" left-text="返回" :fixed="true"
			right-text="" :title="toName"></uni-nav-bar>
		<trtc-room @customEvent="customEvent" 
		@click="itemClick" :user="user" 
		:gjx="gjx" clienttype="1"
		:isCustomTitile="true"
		 ref="trtc" >
		</trtc-room>
	</view>
</template>


<script>
	export default {
		data() {
			return {
				user: {},
				userId: '',
				businessId: 0,
				patientId: 0,
				touserId: '',
				roomId: 0,
				patName: '',
				docName: '',
				orderNo: '',
				otherUrl: '',
				userUrl: '',
			 
				iscan: 0,
				ownImg: '',
				otherImg: '',
				gjx: [],
				toName: '',
				haveOrder: '',
				order: {},
				serviceType: 0, //1术后复诊 2康复复诊
				
				selfInfo:{}, //自己的对象
				otherInfo:{},//对方的对象
			}
		},
		onShow() {
			let pagearr = getCurrentPages(); //获取应用页面栈
			let currentPage = pagearr[pagearr.length - 1]; //获取当前页面信息
			console.log('option:', currentPage.options) //获取页面传递的信息
			// if (currentPage) {
			// 	let userId = currentPage.options.userId;
			// 	let touser = currentPage.options.touser;
			// 	if (userId && touser) {
			// 		this.getOrder(userId, touser);
			// 	}
			// }
		},
		methods: {
			toBack: function() {
				var delta = 1;
				try {
					var pages = getCurrentPages(); //当前页
					var beforePage = (pages[pages.length - 2]).route; //上个页面
					// this.$util.showModal(beforePage);
					if (beforePage && beforePage == "pages/im/imRoom") {
						delta = 2;
					}
				} catch (e) {
					//TODO handle the exception
				}

				 this.$util.navigateBack(delta);
			},
			register() {
				//this.
			},

			setUserInfo() {
				var self = this;
				var info = {};
				self.$lobster.userdata.forEach(item=>{
					if(item.userId ==self.userId){
						self.selfInfo = item;
					}
					if(item.userId== self.touserId)
					{
						self.otherInfo = item;
					}
				});
				if(!self.selfInfo.userId ||  !self.otherInfo.userId){
					self.$util.showToast('参数异常');
					return;
				}
				
				// uni.setNavigationBarTitle({
				// 	title: self.otherInfo.user
				// })
				if(!self.toName){
					self.toName = self.otherInfo.user
				}
				info.userId =  self.userId;
				info.touser =  self.touserId;
				info.patId = 0;
				info.patName = "";
				//info.docName = ;
				info.businessId = 0;
				info.userUrl = self.userUrl;
				info.otherUrl = self.otherUrl;
				info.iscan = self.iscan;
				info.ownImg =  self.selfInfo.img;//self.ownImg;
				info.otherImg = self.otherInfo.img;;
				setTimeout(function() {
					self.user = info;
					console.log(self.user);
				}, 200);
				 
			},

			//自定义工具箱事件
			customEvent(item) {
				switch (item.event) {
					case 'history':
						this.$util.navigateTo('/pages/im/history?userId=' + this.userId + '&touser=' +
							this.touserId + "&patname=" + this.toName);
						break;
					default:
						break;
				}
			},
			//自定义消息点击事件
			itemClick(item) { 
				console.log(item);
			},
			// //自定义工具箱
			// customTools() {
			// 	var tool = {};

			// 	tool.Img = "/static/im/history.png";
			// 	tool.Text = "历史记录";
			// 	tool.event = "history";
			// 	this.gjx.push(tool);
			// },
		},
		onLoad(options) {
			//this.customTools();
			//this.businessId = options.businessId || ''; //预约唯一标识 必填
			// this.userUrl = options.userurl || ''; //点击头像跳转地址 非必填
			// this.otherUrl = options.otherurl || ''; //点击头像跳转地址 非必填

			var userId = options.userId || '';
			var touser = options.touser || '';
			var toName = options.toName || '';
			console.log(options);
			if (userId && touser) {
				this.userId = userId;
				this.touserId = touser;
				this.toName = toName;
				this.setUserInfo();
			}
		},
	}
</script>
<style lang="scss">
	page {
		background-color: #EDEDED !important;
		height: 100%;
	}
</style>
